iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
AI & Data

想到甚麼打甚麼系列 第 8

找LeetCode上簡單的題目來撐過30天啦(DAY8)

  • 分享至 

  • xImage
  •  

題號;100 標題:Same Tree 難度;Easy

Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

Example 1:

Input: p = [1,2,3], q = [1,2,3]
Output: true

Example 2:

Input: p = [1,2], q = [1,null,2]
Output: false

Example 3:

Input: p = [1,2,1], q = [1,1,2]
Output: false

Constraints:

The number of nodes in both trees is in the range [0, 100].
-104 <= Node.val <= 104


我的程式碼

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool checkR();

bool checkL(struct TreeNode* a, struct TreeNode* b){
    if((a == NULL &&  b != NULL)||(a != NULL &&  b == NULL) ){
        return false;
    }else if(a == NULL &&  b == NULL) {
        return true;
    }else if(a->val != b->val){
        return false;
    }else{
       return (checkR(a->right,b->right) && checkL(a->left,b->left)); 
    }
    return true;
}

bool checkR(struct TreeNode* c, struct TreeNode* d){
    if((c == NULL &&  d != NULL)||(c != NULL &&  d == NULL) ){
        return false;
    }else if(c == NULL &&  d == NULL){
        return true;
    }else if(c->val != d->val){
        return false;
    }else{
       return (checkR(c->right,d->right) && checkL(c->left,d->left)); 
    }
    return true;
}

bool isSameTree(struct TreeNode* p, struct TreeNode* q){
    if((p == NULL &&  q != NULL)||(p != NULL &&  q == NULL) ){
        return false;
    }else if(p == NULL &&  q == NULL){
        return true;
    }else if(p->val != q->val){
        return false;
    }else{
        return (checkL(p->left,q->left) && checkR(p->right,q->right));
    }
    return true;
}

邏輯
檢查每個節點的左邊跟右邊是否相等,不相等就return false

花比較多的時間在
資料結構的理解跟遞迴

題號:620 標題:Not Boring Movies 難度:Easy

Table: Cinema

id is the primary key for this table.
Each row contains information about the name of a movie, its genre, and its rating.
rating is a 2 decimal places float in the range [0, 10]

Write an SQL query to report the movies with an odd-numbered ID and a description that is not "boring".

Return the result table in descending order by rating.

The query result format is in the following example:

Cinema table:

Result table:

We have three movies with odd-numbered ID: 1, 3, and 5. The movie with ID = 3 is boring so we don't include it in the answer.


SQL指令

select * from Cinema where ((id%2) <> 0 ) and description <> "boring"  order by rating  desc;

DAY8心得
上班好累/images/emoticon/emoticon09.gif


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY7)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY9)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言